package com.yinhai.adapter;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yinhai.activity.R;
import com.yinhai.activity.SMSDetailActivity;
import com.yinhai.model.PhoneRecord;

import java.util.List;

public class DialAdapter extends BaseAdapter {

	private Activity ctx;
	private List<PhoneRecord> list;
	private LayoutInflater inflater;

	private static  PhoneRecord Recordss =null;
	public DialAdapter(Activity context, List<PhoneRecord> list) {
		this.ctx = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.dial_list_item, null);
			holder = new ViewHolder();
			holder.header = (ImageView) convertView
					.findViewById(R.id.dial_head_pic);
			holder.name = (TextView) convertView
					.findViewById(R.id.dial_item_name);
			holder.number = (TextView) convertView
					.findViewById(R.id.dial_item_num);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}


		PhoneRecord phoneRecord = list.get(position);
//
//		if(!TextUtils.isEmpty(phoneRecord.headerUlr.toString())){
//			Log.e("lishm","头像  "+phoneRecord.headerUlr);
//		}

		Glide.with(ctx).load(phoneRecord.headerUlr).placeholder(R.drawable.tel_photo).into(holder.header);
		holder.name.setText(phoneRecord.getName());
		holder.number.setText(phoneRecord.getTelPhone());


		return convertView;
	}

	public static class ViewHolder {
		ImageView header;
		TextView name;
		TextView number;
//		TextView date;
//		TextView time;
//		ImageView msg_btn;

		public String getTelPhone() {
			return (String) number.getTag();
		}
		public String getName(){
			return name.getText().toString();
		}
	}

	// 通过电话号码异步查询thread_id
	private static class MyAsynQueryHandler extends AsyncQueryHandler {
		private Context contextt;

		public MyAsynQueryHandler(Context context) {
			super(context.getContentResolver());
			contextt = context;
		}

		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			super.onQueryComplete(token, cookie, cursor);
			if (cursor!=null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				long threadId = cursor.getLong(cursor
						.getColumnIndex("thread_id"));
				String address = cursor.getString(cursor
						.getColumnIndex("address"));
				cursor.close();
				Intent intent = new Intent(contextt, SMSDetailActivity.class);
				intent.putExtra("threadId", String.valueOf(threadId));
				intent.putExtra("address", address);
				intent.putExtra("displayName", Recordss.getName());
				contextt.startActivity(intent);
			}else{
                
                Intent intent = new Intent(contextt, SMSDetailActivity.class);
                intent.putExtra("threadId", "");
                intent.putExtra("address", Recordss.getTelPhone());
                intent.putExtra("displayName", Recordss.getName());
                contextt.startActivity(intent);
			}
		}

	};

	private void addViewListener(View view, final PhoneRecord phoneRecord,
			final int position) {
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String telNo = phoneRecord.getTelPhone();
				Uri uri = Uri.parse("content://sms");
				String[] projection = new String[] { "thread_id", "address" }; // 查询的列
				new MyAsynQueryHandler(ctx).startQuery(0, null, uri,
						projection, "address = " + telNo, null, null);

				// Uri uri = Uri.parse("smsto:" + );
				// Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				// ctx.startActivity(intent);
			}
		});
	}
}
